<?php
namespace App\Http\Controllers\Business\Admin\Area;

use Auth;
use App\Models\Province;
use App\Models\City;
use App\Models\District;
use App\Models\BusinessArea;
use Exception;
use Illuminate\Support\Facades\Redis;


class AreaManagement{
    //地区列表
    public static function areaList($type, $parentId='') {
        if($type == 'province') {
            //省
            $data = Province::where('status', '1')->get();
        } elseif($type == 'city') {
            //市
            if(!$parentId) {
                return ['status'=>'failed', 'message'=>'参数错误'];
            }
            $data = City::where('provinceid', $parentId)->get();
        } elseif($type == 'district') {
            //区县
            if(!$parentId) {
                return ['status'=>'failed', 'message'=>'参数错误'];
            }
            $data = District::where('cityid', $parentId)->get();
        } elseif($type == 'businessarea') {
            //商圈
            if(!$parentId) {
                return ['status'=>'failed', 'message'=>'参数错误'];
            }
            $data = BusinessArea::where('districtid', $parentId)->get();
        } else {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //通过城市ID获取省份信息和城市信息，区县列表
    public static function getProvinceAndCityByCityId($cityId) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $city = City::where('id', $cityId)->first();
        if(!$city) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $provinceId = $city->provinceid;
        $province = Province::where('id', $provinceId)->first();
        if(!$province) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $districtLists = District::where('cityid', $cityId)->select('id', 'name')->get();
        $data = [
            'provinceid' => $provinceId,
            'provincename' => $province->name,
            'cityid' => $cityId,
            'cityname' => $city->name,
            'districtlists' => $districtLists
        ];
        return ['status'=>'success', 'data'=>$data ];
    }
    //新增省份， 城市，区县，商圈
    public static function addAreaByType($type, $name, $parentId) {
        if(!$type||!$name || (($type != 'province')&& !$parentId)) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if($type == 'province') {
            if(Province::where('name', $name)->get()->count()>0) {
                return ['status'=>'failed', 'message'=>'参数错误'];
            }
            $createProvince = Province::create([
                'name' => $name,
                'letter' => 'A',
                'status' => 1
            ]);
            $createProvince->save();
        } elseif($type == 'city') {
            $createCity = City::create([
                'name' => $name,
                'provinceid' => $parentId,
                'letter' => 'A',
                'status' => 1
            ]);
            $createCity->save();
        } elseif($type == 'district') {
            $createDistrict = District::create([
                'name' => $name,
                'cityid' => $parentId,
                'letter' => 'A',
                'status' => 1
            ]);
            $createDistrict->save();
        } elseif($type == 'businessarea') {
            $createBusinessArea = BusinessArea::create([
                'name' => $name,
                'districtid' => $parentId,
                'letter' => 'A',
                'status' => 1
            ]);
            $createBusinessArea->save();
        } else {
            return ['status'=>'failed', 'message'=>'类型错误'];
        }
        return ['status'=>'success', 'message'=>'新增成功'];
    }
    //获取所有的身份
    public static function getAllProvince() {
        $data = Province::select('id as provinceid', 'name', 'status')->get();
        return ['status'=>'success', 'data'=>$data];
    }
}